package com.ruoyi.wechat.service.impl;

import com.ruoyi.wechat.common.ServiceException;
import com.ruoyi.wechat.domain.AppCoupon;
import com.ruoyi.wechat.domain.AppUser;
import com.ruoyi.wechat.domain.AppUserCoupon;
import com.ruoyi.wechat.mapper.AppUserCouponMapper;
import com.ruoyi.wechat.service.IAppCouponService;
import com.ruoyi.wechat.service.IAppUserCouponService;
import com.ruoyi.wechat.utils.LoginHelper;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户优惠券Service业务层处理
 *
 * @author YInHeng
 * @date 2024-05-07
 */
@Service
public class AppUserCouponServiceImpl implements IAppUserCouponService {
    @Autowired
    private AppUserCouponMapper appUserCouponMapper;
    @Autowired
    private IAppCouponService couponService;

    /**
     * 查询用户优惠券
     *
     * @param id 用户优惠券主键
     * @return 用户优惠券
     */
    @Override
    public AppUserCoupon selectAppUserCouponById(Long id) {
        AppUser appUser = LoginHelper.getSession().getAppUser();
        AppUserCoupon appUserCoupon = appUserCouponMapper.selectAppUserCouponById(id);
        if (appUserCoupon != null && !appUserCoupon.getUserId().equals(appUser.getId())) {
            throw new ServiceException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "没有权限");
        }
        return appUserCoupon;
    }

    /**
     * 查询用户优惠券列表
     *
     * @param appUserCoupon 用户优惠券
     * @return 用户优惠券
     */
    @Override
    public List<AppUserCoupon> selectAppUserCouponList(AppUserCoupon appUserCoupon) {
        AppUser appUser = LoginHelper.getSession().getAppUser();
        appUserCoupon.setUserId(appUser.getId());
        appUserCoupon.setNum(0);
        List<AppUserCoupon> userCouponList = appUserCouponMapper.selectAppUserCouponList(appUserCoupon);
        //获取消费券标题
        for (AppUserCoupon userCoupon : userCouponList) {
            AppCoupon coupon = couponService.selectAppCouponById(userCoupon.getCouponId());
            userCoupon.setAppCoupon(coupon);
        }
        return userCouponList;
    }

    /**
     * 新增用户优惠券
     *
     * @param appUserCoupon 用户优惠券
     * @return 结果
     */
    @Override
    public int insertAppUserCoupon(AppUserCoupon appUserCoupon) {
        appUserCoupon.setCreateTime(LocalDateTime.now());
        return appUserCouponMapper.insertAppUserCoupon(appUserCoupon);
    }

    /**
     * 修改用户优惠券
     *
     * @param appUserCoupon 用户优惠券
     * @return 结果
     */
    @Override
    public int updateAppUserCoupon(AppUserCoupon appUserCoupon) {
        appUserCoupon.setUpdateTime(LocalDateTime.now());
        return appUserCouponMapper.updateAppUserCoupon(appUserCoupon);
    }

    /**
     * 批量删除用户优惠券
     *
     * @param ids 需要删除的用户优惠券主键
     * @return 结果
     */
    @Override
    public int deleteAppUserCouponByIds(Long[] ids) {
        return appUserCouponMapper.deleteAppUserCouponByIds(ids);
    }

    /**
     * 删除用户优惠券信息
     *
     * @param id 用户优惠券主键
     * @return 结果
     */
    @Override
    public int deleteAppUserCouponById(Long id) {
        return appUserCouponMapper.deleteAppUserCouponById(id);
    }
}
